<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@include file="../../common/common_admin.jsp" %>
<title>产品选择</title>
</head>

<script type="text/javascript">
$(function() {
	initItemList(1);
});
//初始化数据列表
function initItemList(pageNum) {
	$.ajax({
		url: '<%=request.getContextPath() %>/admin/ajaxQueryProduct.action',
		data: {'pageNum': pageNum},
		type: 'post',
		dataType: 'json',
		async: false,
		success: ajaxQueryProduct_Callback
	});
}
function ajaxQueryProduct_Callback(res) {
	var rs = res.result;
	if(rs == 0) {
		var data = res.data;
		var totalCnt = res.totalCnt;
		var totalPageCnt = res.totalPageCnt;
		var pageNum = res.pageNum;
		var itemListHtmlStr = '';
		if(data.length > 0) {
			/*列表*/
			itemListHtmlStr += '<thead>'+
							'<tr>'+
								'<th width="50"><input type="checkbox"></th>'+
								'<th width="100">产品ID</th>'+
								'<th width="200">产品名称</th>'+
								'<th width="100">花材</th>'+
								'<th width="100">颜色</th>'+
								'<th width="100">排序号</th>'+
								'<th width="200">备注</th>';
							'</tr>'+
						'</thead>';
			for(var i=0;i<data.length;i++) {
				itemListHtmlStr += 
							'<tr>'+
								'<td>'+
									'<input name="selectProdCheckbox" type="checkbox" '+
										'value="'+data[i].productId+','+data[i].productName+','+data[i].varietyName+','+data[i].colorName+','+data[i].remarks+'">'+
								'</td>'+
								'<td>'+
									data[i].productId+
								'</td>'+
								'<td>'+
									data[i].productName+
								'</td>'+
								'<td>'+
									data[i].varietyName+
								'</td>'+
								'<td>'+
									data[i].colorName+
								'</td>'+
								'<td>'+
									data[i].sortId+
								'</td>'+
								'<td>'+
									data[i].remarks+
								'</td>'+
							'</tr>';
			}
		}else {
			itemListHtmlStr += '<font color="red">当前类别暂无数据项，请添加！</font>';
		}
		$('#itemListTable').html(itemListHtmlStr);
		
		/*分页*/
		var pagingHtmlStr = queryPagingModule(pageNum,totalPageCnt,totalCnt);
		$('#pagingDiv').html(pagingHtmlStr);
	}else {
		alert("初始化数据失败！");
	}
}

//跳转到指定页码
function goTargetPageNum(targetPageNum) {
	initItemList(targetPageNum);
}
function checkToTargetPage(totalPageNum) {
	var inputPageNum = $('#targetPageInput').val();
	//判断是否为空--是否为整数--是否为正数--是否为有效页数内的正整数
	if( isNaN(inputPageNum) || (parseInt(inputPageNum) != inputPageNum) || 
			(Math.abs(parseInt(inputPageNum)) != parseInt(inputPageNum)) ||  (inputPageNum > totalPageNum) || (inputPageNum < 1 )) {
		alert("请输入查询范围内的正整数页码！");
		return;
	}else {
		goTargetPageNum(inputPageNum);
	}
}
//ajax搜索产品
function ajaxSearchProduct() {
	var productName = $('#productName').val();
	$.ajax({
		url: '<%=request.getContextPath() %>/admin/ajaxSearchProduct.action',
		data: {'productName': productName},
		type: 'post',
		dataType: 'json',
		async: false,
		success: ajaxSearchProduct_Callback
	});
	function ajaxSearchProduct_Callback(res) {
		
	}
}

</script>

<body>
	<div class="displayPageHeader">当前位置：产商管理 >> 商品添加>> 产品选择</div>
	<div class="pageDiv">
		<div class="pageHeadDiv">产品搜索</font></div>
		<div class="infoDiv">
			<form id="itemForm" action="#none" method="post">
				<table>
					<tr>
						<td>&nbsp;&nbsp;产品名称&nbsp;&nbsp;</td>
						<td><input type="text" name="productName" id="productName" style="width: 150px;"></td>
					</tr>
					<tr>
						<td></td>
						<td>
							<input type="button" onclick="ajaxSearchProduct()" value="查询搜索" style="margin-top: 5px;">
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<div class="pageDiv">
		<div class="pageHeadDiv">产品选择</div>
		<div class="infoDiv">
			<div class="itemListDiv">
				<table border="1px" id="itemListTable">
					
				</table>
			</div>
			<div class="margin_5"></div>
			<div id="pagingDiv" style="border: 1px solid #CCC;width: 100%;height: 25px;line-height: 25px;text-align: left;">
				
			</div>
		</div>
	</div>
	<div class="margin_10"></div>
	<div class="pageDiv">
		<input type="button" onclick="closeProductSelect()" value="关闭">
		<input type="button" onclick="selectProduct()" value="确定选择">
	</div>
	<div class="margin_50"></div>
</body>

<script type="text/javascript">
	//父窗口对象
	var parentWindow = window.dialogArguments;
	//关闭产品选择窗口
	function closeProductSelect() {
		//var goodsName = parentWindow.document.getElementById("goodsName");
		//goodsName.value = 'test';
		//window.returnValue = 'test';
		window.close();
	}
	function selectProduct() {
		//var selProdCheckboxArr = $('input[name="selectProdCheckbox"]:checked').val([]);
		var selectCheckboxs = $('input[name="selectProdCheckbox"]:checked');
		if(selectCheckboxs.length < 1) {
			alert("请先选择产品！");
			return;
		}else {
			//封装产品选择的json对象
			var selProdJsonStr = "{\"selResult\":[";
			selectCheckboxs.each(function(){
				var item = $(this).val();
				var itemStr = item.split(',');
				selProdJsonStr += "{\"productId\":\""+itemStr[0]+"\",";
				selProdJsonStr += "\"productName\":\""+itemStr[1]+"\",";
				selProdJsonStr += "\"varietyName\":\""+itemStr[2]+"\",";
				selProdJsonStr += "\"colorName\":\""+itemStr[3]+"\",";
				selProdJsonStr += "\"remarks\":\""+itemStr[4]+"\"},";
			});
			selProdJsonStr = selProdJsonStr.substring(0,selProdJsonStr.length-1);
			selProdJsonStr += "]}";
			//调用父窗口方法传回值
			parentWindow.returnSelProd(selProdJsonStr.toString());
		}
	}
</script>

</html>